package com.fernfx.xingtan.my.presenter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.ShowSettingInfoContract;
import com.fernfx.xingtan.my.model.ShowSettingInfoModel;
import com.fernfx.xingtan.my.ui.MySettingActivity;
import com.fernfx.xingtan.my.ui.ShowSettingInfoActivity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.SPUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

/**
 * Created by admin on 2018/8/14.
 */

public class ShowSettingInfoPresenter<P extends ShowSettingInfoContract.View> implements ShowSettingInfoContract.Presenter {
    private P P;
    private ShowSettingInfoContract.Model model;

    @Override
    public void init(BaseView P) {
        this.P = (P) P;
        this.model = new ShowSettingInfoModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
    }

    @Override
    public void detachView() {
        if (null != model) {
            model = null;
        }
    }


    @Override
    public void displayAppointView(ViewGroup showSettingInfoVp, int viewId) {
        int viewsCount = showSettingInfoVp.getChildCount();
        for (int i = 0; i < viewsCount; i++) {
            View itemVw = showSettingInfoVp.getChildAt(i);
            if (null != itemVw && viewId == itemVw.getId()) {
                itemVw.setVisibility(View.VISIBLE);
            } else if (null != itemVw && LinearLayoutCompat.class.getSimpleName().equals(itemVw.getClass().getSimpleName())) {
                itemVw.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void updateNickName(final Map<String, Object> requestArgsMap, final String nickName) {
        model.updateNickName(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(), 500);
                    onNetworkError(result.getMessage(), 500);
                    return;
                }
                //修改昵称成功了之后做的逻辑
                BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
                if (null != baseEntity && OtherUtil.checkRequestStatus(baseEntity.getSuccess(), baseEntity.getStatus())) {
                    ToastUtil.showCentertoast("昵称修改成功");
                    //设置返回intent
                    Intent dataIntent = new Intent();
                    dataIntent.putExtra(MySettingActivity.NICK_NAME_TEXT, nickName);
                    //检查键盘是否关闭
                    SoftInputUtil.hideKeyboard((BaseActivity) P.getActivity());
                    //刷新上个页面
                    P.getActivity().setResult(Activity.RESULT_OK, dataIntent);
                    P.getActivity().finish();
                } else {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage, errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())) {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public void updateSex(Map<String, Object> requestArgsMap, final String sexText) {
        model.updateSex(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(), 500);
                    onNetworkError(result.getMessage(), 500);
                    return;
                }
                //修改昵称成功了之后做的逻辑
                BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
                if (null != baseEntity && OtherUtil.checkRequestStatus(baseEntity.getSuccess(), baseEntity.getStatus())) {
                    ToastUtil.showCentertoast("更新成功");
                    //设置返回intent
                    Intent dataIntent = new Intent();
                    dataIntent.putExtra(MySettingActivity.SEX_TEXT, sexText);
                    P.getActivity().setResult(Activity.RESULT_OK, dataIntent);
                    P.getActivity().finish();
                } else {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage, errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())) {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public void bussinessCard(Map<String, Object> requestArgsMap) {
        P.getActivity().showLoading();
        model.bussinessCard(requestArgsMap, new BaseIRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                P.getActivity().hideLoading();
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(), 500);
                    onNetworkError(result.getMessage(), 500);
                    return;
                }
                BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
                if (null != baseEntity && OtherUtil.checkRequestStatus(baseEntity.getSuccess(), baseEntity.getStatus())) {
                    if (TextUtils.isEmpty(baseEntity.getMsg())) {
                        return;
                    }
                    String base64Text = baseEntity.getMsg();

                    //将二维码保存在本地
                    SPUtil.writeString(((ShowSettingInfoActivity) P).getQrcodeKey(), base64Text);
                    //显示二维码图片
                    P.showWoQrcode(base64Text);
                } else {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }
        });
    }

    @Override
    public void updateAutograph(Map<String, Object> requestArgsMap,final String autographText) {
        model.updateAutograph(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(), 500);
                    onNetworkError(result.getMessage(), 500);
                    return;
                }
                BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
                if (null != baseEntity && OtherUtil.checkRequestStatus(baseEntity.getSuccess(), baseEntity.getStatus())) {
                    //设置返回intent
                    Intent dataIntent = new Intent();
                    //检查键盘是否关闭
                    SoftInputUtil.hideKeyboard((BaseActivity) P.getActivity());

                    dataIntent.putExtra(MySettingActivity.AUTOGRAPH_TEXT, autographText);
                    P.getActivity().setResult(Activity.RESULT_OK, dataIntent);
                    P.getActivity().finish();
                }else {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage, errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())) {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }
}
